package com.tc.vms.producer;

import com.tc.vms.VMSClient;
import com.tc.vms.iface.VmsMetaData;
import com.tc.vms.utils.Utils;
import com.tc.vms.utils.VmsDomainAttr;
import com.tc.vms.utils.ZkUtils;

import java.util.List;
import java.util.Properties;

/**
 * Created by yonghua.zhang on 2015/12/10.
 */
public final class ChannelMetaData extends VmsMetaData {
    //equals to kafka topic or rabbitmq exchange
    private final String channelName;
    private final String producerInstName;
    //realChlNmOnBroker maybe change, so defined volatile
    private final String realChlNmOnBroker;

    public ChannelMetaData(VmsDomainAttr vmsDomainAttr, String channelName, String clientId, Properties props) {
        super(vmsDomainAttr, props, clientId);
        this.channelName = channelName;
        this.realChlNmOnBroker = ZkUtils.getVmsChannelName(cfgCenterProps);
        this.producerInstName = Utils.normalize(Utils.mkString("-", VMSClient.getVmsVersion(), "channel", belongAttr.getBelongDomain(),
                belongAttr.getBelongAppname(), Utils.getHostIp(), String.valueOf(Utils.getPid()), origClientId));
    }

    public String getChannelName() {
        return channelName;
    }

    public String getRealChlNmOnBroker() {
        return realChlNmOnBroker;
    }

    public String getProducerInstName() {
        return producerInstName;
    }

    public List<String> getChannelSubQueues() {
        String shardsStr = ZkUtils.getVmsChannelSubQueues(cfgCenterProps);
        return Utils.split(shardsStr, ",");
    }

    public boolean isSharding() {
        return ZkUtils.isVmsQueueSharding(cfgCenterProps);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChannelMetaData that = (ChannelMetaData) o;
        if (!super.equals(that)) return false;
        if (!channelName.equals(that.channelName)) return false;
        return producerInstName.equals(that.producerInstName);

    }

    @Override
    public int hashCode() {
        int result = channelName.hashCode();
        result = 31 * result + producerInstName.hashCode();
        return result;
    }

    @Override
    public String getZnodeName() {
        return channelName;
    }

    @Override
    public String getClientInstName() {
        return producerInstName;
    }

    @Override
    public String toString() {
        return "ChannelMetaData{" +
                "channelName='" + channelName + '\'' +
                ", producerInstName='" + producerInstName + '\'' +
                ", realChlNmOnBroker='" + realChlNmOnBroker + '\'' +
                '}';
    }
}
